# -*- coding: utf-8 -*-
#
# SConstruct script for building MahJong-Night server.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# Author: VestniK (Sergey N.Vidyuk)
# Date: 18 Jun 2009
import os
from checkers import *

BaseEnv = Environment(tools=[],ENV=os.environ)
BaseEnv['package'] = 'mahjong-night'
BaseEnv['VERSION'] = '0.0.0svn'

vars = Variables('%s_build.conf'%BaseEnv['PLATFORM'])
vars.Add('CCFLAGS','Custom C compiler flags','')
vars.Add('CPPFLAGS','Custom C/C++ preprocessor flags','')
vars.Add('CXXFLAGS','Custom C++ compiler flags','')
vars.Add('LINKFLAGS','Custom linker flags','')
vars.Add('prefix','install prefix','')
vars.Add('prefix_bin','binaries install prefix','')
vars.Add('prefix_lib','libraries install prefix','')
vars.Add('prefix_pc','pkg-config files install prefix','')
vars.Add('prefix_inc','headers install prefix','')
vars.Add('prefix_data','package data install prefix','')
vars.Add('QJson','QJson library path live blank to detect it using pkg-config','')
vars.Add('QRemoteSignal','QRemoteSignal library path live blank to detect it using pkg-config','')
vars.Update(BaseEnv)
# Hack: need to convert flags lists from strings to lists
BaseEnv['CCFLAGS'] = Split(BaseEnv['CCFLAGS'])
BaseEnv['CPPFLAGS'] = Split(BaseEnv['CPPFLAGS'])
BaseEnv['CXXFLAGS'] = Split(BaseEnv['CXXFLAGS'])
BaseEnv['LINKFLAGS'] = Split(BaseEnv['LINKFLAGS'])
vars.Save('%s_build.conf'%BaseEnv['PLATFORM'],BaseEnv)

if BaseEnv["PLATFORM"] == 'win32':
   BaseEnv.Tool('mingw')
   BaseEnv['LINKFLAGS']+=['-Wl,-subsystem,windows']
else:
   BaseEnv.Tool('default')
BaseEnv.Tool('qt4')
BaseEnv.Tool('qrs')
BaseEnv.Tool('smartinstall')

Help( vars.GenerateHelpText(BaseEnv) )

try:
   if BaseEnv['QJson'] != '':
      BaseEnv.Append( LIBPATH=os.path.join(env['QJson'],'lib') )
      BaseEnv.Append( LIBS='qjson' )
      BaseEnv.Append( CPPPATH=os.path.join(env['QJson'],'include') )
   else:
      BaseEnv.ParseConfig('pkg-config --libs --cflags QJson')
   if BaseEnv['QRemoteSignal'] != '':
      BaseEnv.Append( LIBS='QRemoteSignal' )
      BaseEnv.Append( LIBPATH=os.path.join(env['QRemoteSignal'],'lib') )
      BaseEnv.Append( CPPPATH=os.path.join(env['QRemoteSignal'],'include') )
      BaseEnv.Append( CPPPATH=os.path.join(env['QRemoteSignal'],'include','qremotesignal') )
   else:
      BaseEnv.ParseConfig('pkg-config --libs --cflags QRemoteSignal')
except OSError: pass


BaseEnv['CXXFILESUFFIX']='.cpp'
BaseEnv['QT4_UICDECLPREFIX'] = 'ui_'

# Testing configuration
if not (ARGUMENTS.get('nocheck') or GetOption('clean') or GetOption('help') ) :
   conf = Configure(BaseEnv.Clone(), custom_tests = mjn_tests)

   if not conf.CheckCXX(): Exit(1)
   if not conf.CheckQt4Version("4.4.0"): Exit(1)
   if not conf.CheckQt4Tool('moc'): Exit(1)
   if not conf.CheckQt4Tool('uic'): Exit(1)
   if not conf.CheckQt4Module('QtCore'): Exit(1)
   if not conf.CheckQt4Module('QtXml'): Exit(1)
   if not conf.CheckQt4Module('QtNetwork'): Exit(1)
   if not conf.CheckQt4Module('QtGui'): Exit(1)
   if not conf.CheckQt4Module('QtOpenGL'): Exit(1)
   if not conf.CheckQt4Module('QtSvg'): Exit(1)
   if not conf.CheckQt4Module('QtTest'): Exit(1)
   if not conf.CheckLibWithHeader('QRemoteSignal','QRemoteSignal','c++'): Exit(1)

   conf.Finish()

# building server
SrvLib = SConscript( ['server/SConscript'],exports='BaseEnv' )
# building client
ClientLib = SConscript( ['client/SConscript'],exports='BaseEnv SrvLib' )
#building tests
SConscript( ['tests/SConscript'],exports='BaseEnv SrvLib ClientLib' )
